package com.auto.reg.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class RefreshDownloadUtil {
	
	private static IniEditor editor;
    private static Queue<String> avaliableAccount;
    private static Set<String> downloadHistory;
    
    static {
    	initIniEditor();
    	loadDownloadLog();
    	loadAccount();
    }
    
    public static void refreshDownloadIni() {
    	
    	String result[] = pickOneAccount().split(",");
    	if (result.length == 2) {
    		writeToIni(result[0], result[1], RegConstant.DOWNLOAD_APP_NAME);
    		save(result[0], result[1]);
    	}
    }
    
    private static String pickOneAccount() {
    	
    	String data = "";
    	if (avaliableAccount.size() == 0) {
    		loadAccount();
    	}
    	
    	if (avaliableAccount.size() > 0) {
    		data = avaliableAccount.poll();
    	}
    	return data;
    }
    
    private static void writeToIni(String userName, String passWord, String searchKey) {
    	
    	editor.set(RegConstant.DOWNLOAD, RegConstant.DOWNLOAD_USERNAME, userName  + "@163.com");
    	editor.set(RegConstant.DOWNLOAD, RegConstant.DOWNLOAD_PASSWORD, passWord);
    	editor.set(RegConstant.APP, RegConstant.APP_SEARCHKEY, searchKey);
    	
		try {
			editor.save(RegConstant.DOWNLOAD_INI);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
	private static void initIniEditor() {
		
		editor = new IniEditor();
		try {
			editor.load(new File(RegConstant.DOWNLOAD_INI));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void loadDownloadLog() {
		
		if (null == downloadHistory) {
			downloadHistory = new HashSet<String> ();
		}
		
		BufferedReader readFile = null;
		String line = "";
		
		try {
			readFile = new BufferedReader(new FileReader(new File(RegConstant.DOWNLOAD_LOG)));
			while((line = readFile.readLine()) != null) {
				downloadHistory.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (readFile != null) {
					readFile.close();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}
	
	private static void save(String accountID, String password) {
		
		PrintWriter writeToFile = null;
		try {
			writeToFile = new PrintWriter(new FileOutputStream(RegConstant.DOWNLOAD_LOG, true));
			writeToFile.write(accountID + "," + password + "\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (writeToFile != null) {
				writeToFile.flush();
				writeToFile.close();
			}
		}
	}
	
	private static void loadAccount() {
		
		if (null == avaliableAccount) {
			avaliableAccount = new LinkedList<String>();
		} else {
			avaliableAccount.clear();
		}
		
		BufferedReader readAccount = null;
		String readLine = "";
		try {
			readAccount = new BufferedReader(new FileReader(new File(RegConstant.DOWNLOAD_ACCOUNT)));
			while ((readLine = readAccount.readLine()) != null) {
				if (!downloadHistory.contains(readLine)) {
					avaliableAccount.add(readLine);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (readAccount != null) {
					readAccount.close();
				}
			} catch (IOException ie) {
				ie.printStackTrace();
			}
		}
	}

}
